﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using CustomsSystem.BLL;
using Maticsoft.Common;

namespace WEB.contact
{
    public partial class contactView : System.Web.UI.Page
    {
        public DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            //页面载入
            int? id =Convert.ToInt32( Request.QueryString["ID"]);
            if (Request.QueryString["ID"] == null)
            { 
                btnAdd.Visible = false;
            }
            else
            {
                if (null != id)
                {
                    ContactManage contManage = new ContactManage();
                    ds = contManage.GetList("Customer_ID = " + id);
                    ViewState["data"] = (DataTable)ds.Tables[0];
                    gvContact.DataSource = ds;
                    gvContact.DataBind();
                    ViewState["id"] = id;
                }
            }
        }
        protected void btnContSearch_Click(object sender, EventArgs e)
        {
            //搜索键
            ContactManage contManage = new ContactManage();
            if (tbContSearch.Text.Trim() == "")
            {
                Response.Write("<script>alert('请输入查找信息！')</script>");
            }
            else
            {
                ds = contManage.GetList(" Cu_Customer.CustomerName like '%" + tbContSearch.Text.Trim() + "%'");
                ViewState["data"] = (DataTable)ds.Tables[0];
            }
            gvContact.DataSource = ds;
            gvContact.DataBind();
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvContact_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = Convert.ToInt32(gvContact.DataKeys[e.NewEditIndex].Value);
            Response.Redirect("contactEdit.aspx?ID=" + id);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvContact.DataKeys[e.RowIndex].Value);
            ContactManage contactManage = new ContactManage();

            if (contactManage.Delete(id))
            {
                MessageBox.ShowAndRedirect(this, "删除成功", @"contactView.aspx?ID="+ViewState["id"].ToString());
            }
            else
                Response.Write("<Script>alert('删除失败!')</script>");
        }
        /// <summary>
        /// 增加联系人 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //增加联系人
            string id = Request.QueryString["ID"];
            //Session["Customer_ID"] = id;
            Response.Redirect("contactEdit.aspx?AddID= "+id);        
        }
        /// <summary>
        /// 换页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvContact_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvContact.PageIndex = e.NewPageIndex;
            gvContact.DataSource = (DataTable)ViewState["data"];
            gvContact.DataBind();
        }
    }
}
